﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Zero.UControl.Base.Models;
using Zero.UControl.Component.MsgBox;

namespace Zero.UControl.Component
{
    /// <summary>
    /// SearchBox.xaml 的交互逻辑
    /// </summary>
    public partial class SearchBox : UserControl
    {
        public SearchBox()
        {
            InitializeComponent();
        }

        private DynamicQueryItem _currentQueryItem;

        #region 依赖属性
        public ObservableCollection<DynamicQueryItem> DynamicQuery
        {
            get { return (ObservableCollection<DynamicQueryItem>)GetValue(DynamicQueryProperty); }
            set { SetValue(DynamicQueryProperty, value); }
        }

        public static readonly DependencyProperty DynamicQueryProperty =
            DependencyProperty.Register("DynamicQuery", typeof(ObservableCollection<DynamicQueryItem>), typeof(SearchBox), new PropertyMetadata(null));


        public string QueryText
        {
            get { return (string)GetValue(QueryTextProperty); }
            set { SetValue(QueryTextProperty, value); }
        }

        public static readonly DependencyProperty QueryTextProperty =
            DependencyProperty.Register("QueryText", typeof(string), typeof(SearchBox), new PropertyMetadata(""));


        #endregion

        #region 委托方法
        public Action<DynamicQueryItem> DoSearch { get; set; }

        #endregion

        #region 事件方法
        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender is not ComboBox cbBox)
                return;
            _currentQueryItem = cbBox.SelectedItem as DynamicQueryItem;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (_currentQueryItem != null)
            {
                if(string.IsNullOrEmpty(QueryText))
                {
                    MsgBoxManager.Warn("查询内容不能为空");
                    return;
                }    
                _currentQueryItem.Value = QueryText;
                DoSearch?.Invoke(_currentQueryItem);
            }   
        }
        #endregion
    }
}
